package 剑指offer;

public class _43_1_n整数中1出现的次数 {
	//得出的规律：
	//当前位 cur = 0  1的个数为：高位*（10的当前位的的位数的）即high*dight
	//当前位 cur = 1  1的个数为：高位*(10的当前位的的位数的)+low+1 即high*dight+low+1
	//当前位 cur > 1  1的位数为：高位*（10的当前位的的位数的） +digit
    public int countDigitOne(int n) {
    	int dight = 1,res = 0;
    	int high = n/10,cur = n%10,low = 0;
    	while( high != 0 || cur != 0) {
    		if(cur == 0) res += high*dight;
    		else if(cur ==1) res += high*dight+low+1;
    		else res += (high+1)*dight;
			
			low += cur*dight;
			dight *=10;
			cur = high%10;
			high = high/10;		
    	}
		return res;    	
    }

}
